// import { Component } from "XrFrame/xrFrameSystem";
import { reqLogin, reqUserInfo } from "../../api/user";
import { toast } from "../../utils/extendApi";
import { setStorage } from "../../utils/stroage";
import { ComponentWithStore } from "mobx-miniprogram-bindings";
import { userStore } from "../../stores/userstore";

ComponentWithStore({
  storeBindings: {
    store: userStore,
    fields: ["token","userInfo"],
    actions: ["setToken", "setUserInfo"],
  },

  methods: {
    // 点击登录
    login() {
      // 调用 wx.login 获取用户信息
      wx.login({
        success: async ({ code }) => {
          if (code) {
            // 调用接口 API，传入 code 进行登录
            // const res = await reqLogin(code);
            const { data } = await reqLogin(code);
            // console.log(res);

            setStorage("token", data.token);
            this.setToken(data.token);

            this.getUserInfo();

            wx.navigateBack()
          } else {
            // 登录失败后给用户进行提示
            toast({ title: "授权失败，请稍后再试~~~" });
          }
        },
      });
    },

    async getUserInfo() {
      const { data } = await reqUserInfo();

      setStorage("userInfo", data);
      this.setUserInfo(data);
    },
  },
});
